﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class RecruitMoveRequestDAL : IRecruitMoveRequestDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RecruitMoveRequestDAL()
        {
        }

        public IList<RecruitMoveRequestInfo> ListMoveRequest(RecruitMoveRequestInfo reqInfo)
        {
            string strSQL;
            int empSeq = 0;
            if (reqInfo.Employee != null)
                empSeq = reqInfo.Employee.Sequence;

            List<RecruitMoveRequestInfo> infoList = new List<RecruitMoveRequestInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", reqInfo.Sequence);
                dbManager.AddParameters(2, "@EmpSeq", empSeq);

                strSQL = "SELECT m.MOVE_REQ_SEQ, m.EMP_SEQ, m.MOVE_SEQ, m.JOB_SITE_SEQ, m.MOVE_DATE"
                        + ", m.MOVE_REASON, m.REMARKS, m.RECORD_STATUS, m.CREATED_BY, m.UPDATED_BY"
                        + " FROM HRM_RC_MOVE_REQUESTS m"
                        + " WHERE m.RECORD_STATUS not like @DeleteRecord" 
                        + (reqInfo.Sequence > 0 ? " AND m.MOVE_REQ_SEQ = @Sequence" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeDAL empDAL = new EmployeeDAL();
                    EmployeeInfo empInfo = new EmployeeInfo();
                    if (!rdr.IsDBNull(1))
                    {
                        IList<EmployeeInfo> empList = empDAL.ListEmployees(new EmployeeInfo(rdr.GetInt32(1)));
                        empInfo = empList[0];
                    }
                    JobSiteDAL jsDAL = new JobSiteDAL();
                    JobSiteInfo jsInfo = new JobSiteInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<JobSiteInfo> jsList = jsDAL.ListJobSite(new JobSiteInfo(rdr.GetInt32(3)));
                        jsInfo = jsList[0];
                    }
                    RecruitMoveRequestInfo Info = new RecruitMoveRequestInfo(rdr.GetInt32(0)
                            , empInfo
                            , rdr.IsDBNull(2) ? 0 : rdr.GetInt32(2)
                            , jsInfo
                            , rdr.IsDBNull(4) ? null : (Nullable<DateTime>)rdr.GetDateTime(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? "" : rdr.GetString(7)
                            , rdr.IsDBNull(8) ? null : (Nullable<int>)rdr.GetInt32(8)
                            , rdr.IsDBNull(9) ? null : (Nullable<int>)rdr.GetInt32(9)
                            );
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public void Insert(RecruitMoveRequestInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(1, "@JobSite", Info.MoveToJobSite == null ? (int?)null : Info.MoveToJobSite.Sequence);
                dbManager.AddParameters(2, "@SeqNo", Info.SeqNo);
                dbManager.AddParameters(3, "@MoveDate", Info.MoveDate);
                dbManager.AddParameters(4, "@Reason", Info.Reason);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(8, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_RC_MOVE_REQUESTS"
                    + "(EMP_SEQ, MOVE_SEQ, JOB_SITE_SEQ, MOVE_DATE, MOVE_REASON, REMARKS"
                    + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@EmpSeq, @SeqNo, @JobSite, @MoveDate, @Reason, @Remarks"
                        + ", @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(RecruitMoveRequestInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(1, "@JobSite", Info.MoveToJobSite == null ? (int?)null : Info.MoveToJobSite.Sequence);
                dbManager.AddParameters(2, "@SeqNo", Info.SeqNo);
                dbManager.AddParameters(3, "@MoveDate", Info.MoveDate);
                dbManager.AddParameters(4, "@Reason", Info.Reason);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@Sequence", Info.Sequence);
                dbManager.AddParameters(7, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(8, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_RC_MOVE_REQUESTS"
                    + " SET MOVE_SEQ = @SeqNo"
                        + ", JOB_SITE_SEQ = @JobSite"
                        + ", MOVE_DATE = @MoveDate"
                        + ", MOVE_REASON = @Reason"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE MOVE_REQ_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_RC_MOVE_REQUESTS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE MOVE_REQ_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
